//
//  CompanyCell.m
//  Btpin
//
//  Created by Vic on 2019/3/18.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyCell.h"

#define CompanyCellLblTag (100)

@interface CompanyCell ()
{
    UIView *bgView;
    UIImageView *logoImgView;
    UILabel *nameLbl;
    UIImageView *integrityTagImgView;
    UILabel *baseInfoLbl;
    UILabel *addressLbl;
    UIView *lineView;
    UILabel *hotLbl;
    UIView *lineViewBottom;
}
@end

@implementation CompanyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}
- (void)initUI {
    self.backgroundColor = COLOR_OF_CLEAR;
    self.contentView.backgroundColor = COLOR_OF_CLEAR;
    
    bgView = [UIToolsControl initViewWithFrame:CGRectMake(ScalePS(10), 0, WIDTH-ScalePS(20), ScalePS(100)) backgroundColor:COLOR_OF_WHITE];
    bgView.layer.cornerRadius = ScalePS(8);
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView setShadowShadowColor:THEMECOLOR_OF_GRAY shadowOpacity:0.3 shadowRadius:ScalePS(8) shadowOffset:CGSizeMake(0, 3*Scale)];
     
    
    logoImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(ScalePS(10), 20*Scale, 40*Scale, 40*Scale) imageName:@"logo_bg"];
    logoImgView.layer.cornerRadius = 5*Scale;
    logoImgView.layer.masksToBounds = YES;
    [bgView addSubview:logoImgView];
    
    nameLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(logoImgView)+10*Scale, StartY(logoImgView), (WIDTH-90*Scale), 20*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_333333 font:Font_17];
    nameLbl.font = FontBold(Font_17);
    nameLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [bgView addSubview:nameLbl];
    
    integrityTagImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(EndX(nameLbl)+2, StartY(nameLbl)+12*Scale, 12*Scale, 12*Scale) imageName:@"integrity_tag"];
    [bgView addSubview:integrityTagImgView];
    integrityTagImgView.hidden = YES;
    
    baseInfoLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(nameLbl), EndY(nameLbl)+ScalePS(5), ScalePS(230), 15*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_666666 font:Font_12];
    [bgView addSubview:baseInfoLbl];
    
    addressLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(baseInfoLbl), StartY(baseInfoLbl), ScalePS(90), 15*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_10];
    addressLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
    addressLbl.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:addressLbl];
    
    CGFloat lblWidth = (WIDTH-60*Scale)*0.2;
    for (NSInteger i = 0; i < 5; i++) {
        UILabel *lbl = [UIToolsControl initLabelWithFrame:CGRectMake(10*Scale+(lblWidth+5*Scale)*i, EndY(logoImgView)+15*Scale, lblWidth, CompanyTagsHeight) text:nil textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.layer.cornerRadius = 2*Scale;
        lbl.layer.masksToBounds = YES;
//        lbl.layer.borderColor = THEMECOLOR_OF_TEXT_cccccc.CGColor;
        lbl.layer.borderColor = RGB(220, 220, 220).CGColor;
        lbl.layer.borderWidth = 0.5;
        lbl.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:lbl];
        lbl.tag = CompanyCellLblTag+i;
        lbl.hidden = YES;
    }
    
    lineView = [UIToolsControl initViewWithFrame:CGRectMake(StartX(logoImgView), EndY(logoImgView)+55*Scale, WIDTH-30*Scale, lineH) backgroundColor:THEMECOLOR_OF_Line_Color];
    [bgView addSubview:lineView];
    lineView.hidden = YES;
    
    hotLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-40*Scale, 40*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_13];
    [bgView addSubview:hotLbl];
    hotLbl.hidden = YES;
    
    lineViewBottom = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(hotLbl), WIDTH, 15*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
    [bgView addSubview:lineViewBottom];
    lineViewBottom.hidden = YES;
}
- (void)reloadData {
//    [logoImgView sd_setImageWithURL:[NSURL URLWithString:@""]];
    
    NSString *name = @"咕咚";
    CGFloat width = [UIToolsControl textWidth:name size:CGSizeMake(MAXFLOAT, 32*Scale) font:Font_17]+10;
    nameLbl.frame = CGRectMake(EndX(logoImgView)+10*Scale, StartY(logoImgView), width, 20*Scale);
    integrityTagImgView.frame = CGRectMake(EndX(nameLbl)+2, StartY(nameLbl)+12*Scale, 10*Scale, 10*Scale);
    nameLbl.text = name;
    
    baseInfoLbl.text = [NSString stringWithFormat:@"%@ | %@ | %@", @"天使轮", @"50-100人", @"移动互联网"];
    addressLbl.text = @"海淀区-中关村";
    
    NSArray *arr = @[@"班车接送", @"五险一金", @"节日礼物"];
    for (NSInteger i = 0; i < arr.count; i++) {
        UILabel *lbl = (UILabel *)[self viewWithTag:CompanyCellLblTag+i];
        lbl.text = arr[i];
        lbl.hidden = NO;
    }
    
    NSString *jobText = @"高级程序架构师";
    NSString *hotText = [NSString stringWithFormat:@"热招：%@ 等%@个职位", jobText, @"153"];
    NSMutableAttributedString *hotAtt = [[NSMutableAttributedString alloc] initWithString:hotText];
    [hotAtt addAttributes:@{NSFontAttributeName:Font(Font_13), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_9a9a9a} range:NSMakeRange(0, hotText.length)];
    [hotAtt addAttribute:NSForegroundColorAttributeName value:THEMECOLOR_OF_ORANGE range:NSMakeRange(3, jobText.length)];
    hotLbl.attributedText = hotAtt;
}
- (void)reloadDataModel:(CompanyInfoModel *)model {
    bgView.frame = CGRectMake(ScalePS(10), 0, WIDTH-ScalePS(20), model.listCellHeight-ScalePS(15));
    
    if (model.logo && model.logo.length) {
        [logoImgView sd_setImageWithURL:[NSURL URLWithString:model.logo] placeholderImage:[UIImage imageNamed:@"logo_bg"]];
    } else {
        logoImgView.image = [UIImage imageNamed:@"logo_bg"];
    }
    
//    CGFloat width = [UIToolsControl textWidth:model.name size:CGSizeMake(MAXFLOAT, 32*Scale) font:Font_17]+10;
    NSInteger width = ceil([model.name boundingRectWithSize:CGSizeMake(MAXFLOAT, 32*Scale) options:(NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:FontBold(Font_17)} context:nil].size.width)+1;
    nameLbl.frame = CGRectMake(EndX(logoImgView)+10*Scale, StartY(logoImgView), MIN(width, (WIDTH-90*Scale)), 20*Scale);
    integrityTagImgView.frame = CGRectMake(EndX(nameLbl)+2, StartY(nameLbl)+4*Scale, 12*Scale, 12*Scale);
    nameLbl.text = model.name;
    if (model.auth_medal && model.auth_medal.length) {
        if ([model.auth_medal integerValue]) {
            integrityTagImgView.hidden = NO;
        } else {
            integrityTagImgView.hidden = YES;
        }
    }
    
    baseInfoLbl.text = [NSString stringWithFormat:@"%@ | %@ | %@", model.finance_cn, model.scale_cn, model.trade_cn];
    addressLbl.text = model.district_cn;
    
    for (NSInteger i = 0; i < 5; i++) {
        UILabel *lbl = (UILabel *)[self viewWithTag:CompanyCellLblTag+i];
        lbl.hidden = YES;
    }
    if (model.tags && model.tags.count) {
        for (NSInteger i = 0; i < model.tags.count; i++) {
            UILabel *lbl = (UILabel *)[self viewWithTag:CompanyCellLblTag+i];
            lbl.text = model.tags[i];
            lbl.hidden = NO;
        }
    }
    
    if (model.job_num && ([model.job_num integerValue]>0)) {
        hotLbl.hidden = NO;
        NSString *jobText = (model.newjob.title?model.newjob.title:@"");
        NSString *hotText = [NSString stringWithFormat:@"热招：%@ 等%@个职位", jobText, (model.job_num?model.job_num:@"")];
        NSMutableAttributedString *hotAtt = [[NSMutableAttributedString alloc] initWithString:hotText];
        [hotAtt addAttributes:@{NSFontAttributeName:Font(Font_13), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_9a9a9a} range:NSMakeRange(0, hotText.length)];
        [hotAtt addAttribute:NSForegroundColorAttributeName value:THEMECOLOR_OF_ORANGE range:NSMakeRange(3, jobText.length)];
        hotLbl.attributedText = hotAtt;
        
        if (model.tags && model.tags.count) {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+55*Scale, WIDTH-40*Scale, lineH);
            hotLbl.frame = CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-30*Scale, 40*Scale);
            lineViewBottom.frame = CGRectMake(0, EndY(hotLbl), WIDTH, 15*Scale);
        } else {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+15*Scale, WIDTH-40*Scale, lineH);
            hotLbl.frame = CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-30*Scale, 40*Scale);
            lineViewBottom.frame = CGRectMake(0, EndY(hotLbl), WIDTH, 15*Scale);
        }
        lineView.hidden = NO;
    } else {
        hotLbl.hidden = YES;
        if (model.tags && model.tags.count) {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+55*Scale, WIDTH-40*Scale, lineH);
//            hotLbl.frame = CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-30*Scale, 45*Scale);
            lineViewBottom.frame = CGRectMake(0, EndY(logoImgView)+55*Scale, WIDTH, 15*Scale);
        } else {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+15*Scale, WIDTH-40*Scale, lineH);
            lineViewBottom.frame = CGRectMake(0, EndY(logoImgView)+15*Scale, WIDTH, 15*Scale);
        }
        lineView.hidden = YES;
    }
}
- (void)reloadDataModelForCollect:(CompanyInfoModel *)model {
    if (model.logo && model.logo.length) {
        [logoImgView sd_setImageWithURL:[NSURL URLWithString:model.logo] placeholderImage:[UIImage imageNamed:@"logo_bg"]];
    } else {
        logoImgView.image = [UIImage imageNamed:@"logo_bg"];
    }
    
//    CGFloat width = [UIToolsControl textWidth:model.name size:CGSizeMake(MAXFLOAT, 32*Scale) font:Font_17]+10;
    NSInteger width = ceil([model.name boundingRectWithSize:CGSizeMake(MAXFLOAT, 32*Scale) options:(NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:FontBold(Font_17)} context:nil].size.width)+1;
    nameLbl.frame = CGRectMake(EndX(logoImgView)+10*Scale, StartY(logoImgView), MIN(width, (WIDTH-90*Scale)), 20*Scale);
    integrityTagImgView.frame = CGRectMake(EndX(nameLbl)+2, StartY(nameLbl)+4*Scale, 12*Scale, 12*Scale);
    nameLbl.text = model.name;
    if (model.auth_medal && model.auth_medal.length) {
        if ([model.auth_medal integerValue]) {
            integrityTagImgView.hidden = NO;
        } else {
            integrityTagImgView.hidden = YES;
        }
    }
    
    baseInfoLbl.text = [NSString stringWithFormat:@"%@ | %@ | %@", model.finance_cn, model.scale_cn, model.trade_cn];
    addressLbl.text = model.address;
    
    for (NSInteger i = 0; i < 5; i++) {
        UILabel *lbl = (UILabel *)[self viewWithTag:CompanyCellLblTag+i];
        lbl.hidden = YES;
    }
    if (model.tags && model.tags.count) {
        for (NSInteger i = 0; i < model.tags.count; i++) {
            UILabel *lbl = (UILabel *)[self viewWithTag:CompanyCellLblTag+i];
            lbl.text = model.tags[i];
            lbl.hidden = NO;
        }
    }
    
    if (model.job_num && ([model.job_num integerValue]>0)) {
        hotLbl.hidden = NO;
        NSString *jobText = (model.newjob.title?model.newjob.title:@"");
        NSString *hotText = [NSString stringWithFormat:@"热招：%@ 等%@个职位", jobText, (model.job_num?model.job_num:@"")];
        NSMutableAttributedString *hotAtt = [[NSMutableAttributedString alloc] initWithString:hotText];
        [hotAtt addAttributes:@{NSFontAttributeName:Font(Font_13), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_9a9a9a} range:NSMakeRange(0, hotText.length)];
        [hotAtt addAttribute:NSForegroundColorAttributeName value:THEMECOLOR_OF_ORANGE range:NSMakeRange(3, jobText.length)];
        hotLbl.attributedText = hotAtt;
        
        if (model.tags && model.tags.count) {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+55*Scale, WIDTH-40*Scale, lineH);
            hotLbl.frame = CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-30*Scale, 40*Scale);
            lineViewBottom.frame = CGRectMake(0, EndY(hotLbl), WIDTH, 15*Scale);
        } else {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+15*Scale, WIDTH-40*Scale, lineH);
            hotLbl.frame = CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-30*Scale, 40*Scale);
            lineViewBottom.frame = CGRectMake(0, EndY(hotLbl), WIDTH, 15*Scale);
        }
        lineView.hidden = NO;
    } else {
        hotLbl.hidden = YES;
        if (model.tags && model.tags.count) {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+55*Scale, WIDTH-40*Scale, lineH);
//            hotLbl.frame = CGRectMake(StartX(logoImgView), EndY(lineView), WIDTH-30*Scale, 45*Scale);
            lineViewBottom.frame = CGRectMake(0, EndY(logoImgView)+55*Scale, WIDTH, 15*Scale);
        } else {
            lineView.frame = CGRectMake(StartX(logoImgView), EndY(logoImgView)+15*Scale, WIDTH-40*Scale, lineH);
            lineViewBottom.frame = CGRectMake(0, EndY(logoImgView)+15*Scale, WIDTH, 15*Scale);
        }
        lineView.hidden = YES;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
